import {utils,date} from "@/assets/util/index.js";
import {mapActions} from "pinia";

let util = utils();
let dateUtil = date();
import {useCommonStore} from "@/store/common/store.js";

export default {
    name: "Index",
    data() {
        return {
            submitting:false,
            showFilterArea: true,
            pageType: '',
            list: {
                tableData: [],
                pageNo: 1,
                pageSize: 10,
                total: 0
            }
        }
    },
    methods: {
        ...mapActions(useCommonStore, ["selectPage", "selectList", "selectById", "insert", "update", "delete"]),
        handleFilter() {
            this.showFilterArea = !this.showFilterArea
        },
        onCreate(url, title) {
            this.$router.push({
                path: url
            });
            let operators = localStorage.getItem("operators");
            if (operators) {
                operators = JSON.parse(operators);
                util.handleOperators(operators, title, url);
            }
        },
        onSearch(option) {
           return new Promise((resolve, reject)=>{
               this.selectPage(this.pageType, option).then(res => {
                   let data = res.data;
                   this.list.tableData = data.list;
                   this.list.total = data.total;
                   resolve(data);
               }).catch(e=>{
                   reject(e)
               })
           })
        },
        onSubmit(option) {
            return new Promise((resolve, reject)=>{
                this.submitting = true;
                this.insert(this.pageType, option).then(res => {
                    this.$message.success("提交成功");
                    this.submitting = false;
                    resolve(res);
                }).catch(e=>{
                    this.submitting = false;
                    reject(e)
                });
            })
        },
        onEdit(option) {
            return new Promise((resolve, reject)=>{
                this.submitting = true;
                this.update(this.pageType, option).then(res => {
                    this.$message.success("修改成功");
                    this.submitting = false;
                    resolve(res);
                }).catch(e=>{
                    this.submitting = false;
                    reject(e)
                });
            })
        },
        onDelete(id){
            return new Promise((resolve, reject)=> {
                this.delete(this.pageType, id).then(res => {
                    resolve(res);
                }).catch(e=>{
                    reject(e)
                });
            });
        },
        formatDate(d){
            return dateUtil.formatDate(d, "yyyy-MM-DD HH:mm");
        }
    }
}
